#include "LoadConfig.h"
#include <iostream>
#include <fstream>

using namespace std;
using namespace boost::program_options;


IMPL_LOGGER(LoadConfig,logger);

LoadConfig::LoadConfig()
{
    
    m_listen_block = -1;
    m_socket_timeout_sec = 0;
    m_client_cmd_recv_thread_num = 0;
}

LoadConfig::~LoadConfig()
{
    
}

bool LoadConfig::load_config(string filename)
{
  
    
    LOG4CPLUS_DEBUG(logger,"satrt load_config");

    m_file_name = filename;
    
    options_description opts("Bncf means: Basic network communication framework");

    opts.add_options()
        ("info","Bug email: yangqinghua159@qq.com")
        ("server.listen.ip",value<string>()->default_value("0.0.0.0"),"server listen ip")
        ("server.listen.port",value<uint32_t>()->default_value(3459),"server listen port")
        ("server.listen.block",value<int32_t>()->default_value(5),"server listen block")
        ("client.socket.timeout.sec",value<uint32_t>()->default_value(3),"client time out sec")
        ("client.cmd.recv.thread.num",value<uint32_t>()->default_value(1),"client cmd recv thread num");
    


    ifstream ifs(filename.c_str());
    
    variables_map vm;
    
    store(parse_config_file(ifs,opts),vm);

    try
    {
        do
        {
            if(vm.size()==0)
            {
                return false;
            }
            
            LOG4CPLUS_DEBUG(logger,"info:"<<opts);
            
            if(vm.count("server.listen.ip"))
            { 
                LOG4CPLUS_DEBUG(logger,"server.listen.ip:"<<vm["server.listen.ip"].as<string>());
                m_server_ip = vm["server.listen.ip"].as<string>();
            }
           

            if(vm.count("server.listen.port"))
            {
                LOG4CPLUS_DEBUG(logger,"server.listen.port:"<<vm["server.listen.port"].as<uint32_t>());
                m_server_port = vm["server.listen.port"].as<uint32_t>();
            }


            if(vm.count("server.listen.block"))
            {
                LOG4CPLUS_DEBUG(logger,"server.listen.block:"<<vm["server.listen.block"].as<int32_t>());
                m_listen_block = vm["server.listen.block"].as<int32_t>();
            }

            if(vm.count("client.socket.timeout.sec"))
            {
                LOG4CPLUS_DEBUG(logger,"client.socket.timeout.sec:"<<vm["client.socket.timeout.sec"].as<uint32_t>());
                m_socket_timeout_sec= vm["client.socket.timeout.sec"].as<uint32_t>();
            }

            if(vm.count("client.cmd.recv.thread.num"))
            {
                LOG4CPLUS_DEBUG(logger,"client.cmd.recv.thread.num:"<<vm["client.cmd.recv.thread.num"].as<uint32_t>());
                m_client_cmd_recv_thread_num = vm["client.cmd.recv.thread.num"].as<uint32_t>();
            }
            
        }while(0);
        
    }
    catch(...)
    {
        LOG4CPLUS_DEBUG(logger,"excption happen");
        return false;
    }

    LOG4CPLUS_DEBUG(logger,"end load_config");
    
    return true;
}
